#include "dbparachute.h"

DBParachute *g_parachute = 0;
string g_ArchPath = "";

DBParachute::DBParachute()
{
	fhandle = 0;
	fopened = 0;
}

DBParachute::~DBParachute()
{
	//dtor
}

DBParachute *DBParachute::PARA()
{
	if ( g_parachute ) {
		return g_parachute;
	}

	g_parachute = new DBParachute;
	return g_parachute;
}
string DBParachute::timeToName ( time_t sys_time )
{
	string fname;
	char buff[32];
	DBTime td;
	db_specTime st;
	DBTime dbt;
	dbt.FromSystemTime(sys_time);
	st = dbt.translate();

	td.FromSystemTime ( sys_time );
	fname.append ( "PARA_" );
	sprintf ( buff, "%d", st.year );
	fname.append ( buff );

	if ( st.month < 10 ) {
		fname.append ( "-0" );
	}
	else {
		fname.append ( "-" );
	}

	sprintf ( buff, "%d", st.month );
	fname.append ( buff );

	if ( st.day < 10 ) {
		fname.append ( "-0" );
	}
	else {
		fname.append ( "-" );
	}

	sprintf ( buff, "%d", st.day );
	fname.append ( buff );
	fname.append ( ".para" );
	string full = g_ArchPath;

	if ( ( full[full.size() - 1] != '\\' ) && ( full[full.size() - 1] != '/' ) )
	{
		full.push_back ( '/' );
	}

	full.append ( fname  );
	return full;
}

i32 DBParachute::WriteBlock ( time_t sys_time, time_t ext_time, u8 type, void *data, u32 size, string comment )
{
	if ( sys_time / DAY *DAY != fopened )
	{
		if ( fhandle ) {
			fclose ( fhandle );
		}
		string name = timeToName ( sys_time );
		LOGP2 ( "Parachute open file [%s]", name.c_str() );
		fhandle = fopen ( name.c_str(), "ab" );

		if ( !fhandle )
		{
			LOGP1 ( "PARACHUTE: Error opening file" );
			return 0;
		}
	}

	char buff[4096];
	time_t truetime = time ( 0 );

	if ( !comment.size() )
	{
		snprintf ( buff, 4096, "\r\n***$$$%s TY%d,sysT %X, extT %X,size %d $$$\r\n",
				   ctime ( &truetime ), type, ( u32 ) sys_time, ( u32 ) ext_time, size );
	}
	else
    {
		snprintf ( buff, 4096, "\r\n***$$$%s TY%d,sysT %X, extT %X,size %d; COMMENT [%s]$$$\r\n",
				   ctime ( &truetime ), type, ( u32 ) sys_time, ( u32 ) ext_time, size,comment.c_str() );
    }

	i32 ret = fwrite ( buff, strlen ( buff ), 1, fhandle );

	if ( ret != 1 )
	{
		LOGP1 ( "PARACHUTE: Error writing" );
		return 0;
	}

	ret = fwrite ( data, size, 1, fhandle );

	if ( ret != 1 )
	{
		LOGP1 ( "PARACHUTE: Error writing" );
		return 0;
	}

	snprintf ( buff, 4096, "\r\n###END_HERE\r\n" );
	ret = fwrite ( buff, strlen ( buff ), 1, fhandle );

	if ( ret != 1 )
	{
		LOGP1 ( "PARACHUTE: Error writing" );
		return 0;
	}

	fflush ( fhandle );
	return 1;
}

i32 DBParachute::WriteErrCode ( time_t sys_time, i32 errcode, string errstr )
{
	if ( sys_time / DAY *DAY != fopened )
	{
		if ( fhandle ) {
			fclose ( fhandle );
		}

		string name = timeToName ( sys_time );
		LOGP2 ( "Parachute open file [%s]", name.c_str() );
		fhandle = fopen ( name.c_str(), "ab" );

		if ( !fhandle )
		{
			LOGP1 ( "PARACHUTE: Error opening file" );
			return 0;
		}
	}

	char buff[4096];
	time_t truetime = time ( 0 );
	snprintf ( buff, 4096, "\r\n***@@@%s ,sysT %X,error#%d : [%s] $$$\r\n", ctime ( &truetime ), ( u32 ) sys_time, errcode, errstr.c_str() );
	i32 ret = fwrite ( buff, strlen ( buff ), 1, fhandle );

	if ( ret != 1 )
	{
		LOGP1 ( "PARACHUTE: Error writing" );
		return 0;
	}

	fflush ( fhandle );
	return 1;
}


i32 DBParachute::WriteMsg ( time_t sys_time, i32 lvl, string msg )
{
	if ( sys_time / DAY *DAY != fopened )
	{
		if ( fhandle ) {
			fclose ( fhandle );
		}

		string name = timeToName ( sys_time );
		LOGP2 ( "Parachute open file [%s]", name.c_str() );
		fhandle = fopen ( name.c_str(), "ab" );

		if ( !fhandle )
		{
			LOGP1 ( "PARACHUTE: Error opening file" );
			return 0;
		}
	}

	char buff[4096];
	time_t truetime = time ( 0 );
	snprintf ( buff, 4096, "\r\n***@@@%s ,sysT %X,level #%d : [%s] $$$\r\n", ctime ( &truetime ), ( u32 ) sys_time, lvl, msg.c_str() );
	i32 ret = fwrite ( buff, strlen ( buff ), 1, fhandle );

	if ( ret != 1 )
	{
		LOGP1 ( "PARACHUTE: Error writing" );
		return 0;
	}

	fflush ( fhandle );
	return 1;
}
